package com.highbury5.blog.util.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import javax.annotation.PostConstruct;
import java.util.Map;

@Component
public class RedisUtil {

    private static final int SINGLE = 0;
    private static final int CLUSTER = 1;
    private static final int CURRENT = SINGLE;

    private static final int START = 0;
    private static final int END  = 10;

    private AbstractRedisComponent instance = null;

    @Autowired
    ClusterRedisComponent clusterRedisComponent;

    @PostConstruct
    public void  init(){
        if(CURRENT == SINGLE){
            instance = new SingleRedisComponent();
        }else if(CURRENT == CLUSTER){
            instance = clusterRedisComponent;
        }
    }

    /**
     * 向对应的zset中元素score加1,返回最新score
     */
    public double zincrby(String key ,String value){
        return instance.zincrby(key,value);
    }

    public Map<String,Double> zrevrange(String key){
        return zrevrange(key,START,END);
    }

    public Map<String,Double> zrevrange(String key ,int start,int end){
        return instance.zrevrange(key,start,end);
    }

    public String get(String key){
        return instance.get(key);
    }

    public Long incr(String key){
        return instance.incr(key);
    }

}
